package xupt.se.ttms.servlet;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import xupt.se.ttms.dao.EmployeeDAO;
import xupt.se.ttms.dao.UserDAO;
import xupt.se.ttms.idao.DAOFactory;
import xupt.se.ttms.model.Employee;
import xupt.se.ttms.model.User;

@WebServlet("/user/UserServlet")
public class UserServlet extends HttpServlet
{
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
    {
        doPost(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
    {
        request.setCharacterEncoding("utf-8");
        String method = request.getParameter("method");
        if(method.equalsIgnoreCase("add"))
            add(request, response);
        else
            // 登录用户修改自己的信息
            if(method.equalsIgnoreCase("update"))
                update(request, response);
            else
                if(method.equalsIgnoreCase("search"))
                    search(request, response);
                else
                    if(method.equalsIgnoreCase("delete"))
                        delete(request, response);
                    else
                        if(method.equalsIgnoreCase("searchById"))
                            searchById(request, response);
                        else
                            if(method.equalsIgnoreCase("searchByPage"))
                                searchByPage(request, response);
                            else
                                // 管理员修改员工权限
                                if(method.equalsIgnoreCase("updateType"))
                                    updateType(request, response);
    }

    // 管理员只能修改用户权限
    private void updateType(HttpServletRequest request, HttpServletResponse response)
            throws IOException
    {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        String emp_no = request.getParameter("emp_no");
        String type = request.getParameter("type");
        int t;
        if("管理员".equals(type))
        {
            t = 1;
        }
        else
        {
            t = 0;
        }
        UserDAO dao = (UserDAO) DAOFactory.creatUserDAO();
        int result = dao.updateType(emp_no, t);
        User user = new User();
        user.setEmp_no(emp_no);
        user.setType(t);
        try
        {
            if(result > 0)
                request.setAttribute("user", user);
            request.setAttribute("ok", "ok");
            searchByPage(request, response);
            // request.getRequestDispatcher("index.jsp").forward(request, response);
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }

    private void add(HttpServletRequest request, HttpServletResponse response)
    {}

    private void searchByPage(HttpServletRequest request, HttpServletResponse response)
    {
        int currentPage = 1;// 默认当前页第一页
        String strpage = request.getParameter("currentPage"); // 获取前台传入当前页
        if(strpage != null && !strpage.equals(""))
        {
            currentPage = Integer.parseInt(strpage) < 1 ? 1 : Integer.parseInt(strpage); // 将字符串转换成整型
        }
        String emp_no = request.getParameter("emp_no");
        UserDAO dao = (UserDAO) DAOFactory.creatUserDAO();
        // 从UserDAO中获取所有用户信息
        ArrayList<User> list = dao.findUserByPage(currentPage, emp_no);
        // 从UserDAO中获取总记录数
        int allCount = dao.getAllCount();
        // 从UserDAO中获取总页数
        int allPageCount = dao.getAllPageCount();
        // 从UserDAO中获取当前页
        currentPage = dao.getCurrentPage();
        // 存入request中
        request.setAttribute("allUser", list);
        request.setAttribute("allCount", allCount);
        request.setAttribute("allPageCount", allPageCount);
        request.setAttribute("currentPage", currentPage);
        request.setAttribute("search_emp_no", emp_no);

        try
        {
            request.getRequestDispatcher("index.jsp").forward(request, response);
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }

    private void searchById(HttpServletRequest request, HttpServletResponse response)
    {

    }

    private void delete(HttpServletRequest request, HttpServletResponse response)
    {
        int result = 0;
        String emp_no = request.getParameter("emp_no");
        if(null != emp_no && !("".equals(emp_no)))
        {
            UserDAO dao = (UserDAO) DAOFactory.creatUserDAO();
            result = dao.delete(emp_no);
            if(result > 0)
                request.setAttribute("result", "删除成功!");
            else
                request.setAttribute("result", "删除失败!");
            // 分页时删除调用分页全查:使用分页index1.jsp时，把这里注释打开
            searchByPage(request, response);
        }
    }

    // 个人设置修改信息
    private void update(HttpServletRequest request, HttpServletResponse response)
            throws UnsupportedEncodingException
    {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");
        int emp_id = Integer.parseInt(request.getParameter("emp_id"));
        String emp_no = request.getParameter("emp_no");
        String emp_name = request.getParameter("emp_name");
        String emp_pass = request.getParameter("emp_pass");
        String emp_tel_num = request.getParameter("emp_tel_num");
        String emp_addr = request.getParameter("emp_addr");
        String emp_email = request.getParameter("emp_email");
        Employee employee = new Employee();
        employee.setEmp_id(emp_id);
        employee.setEmp_no(emp_no);
        employee.setEmp_name(emp_name);
        employee.setEmp_tel_num(emp_tel_num);
        employee.setEmp_addr(emp_addr);
        employee.setEmp_email(emp_email);
        EmployeeDAO dao = (EmployeeDAO) DAOFactory.creatEmployeeDAO();
        boolean result = dao.update(employee);
        try
        {
            if(result)
            {
                request.setAttribute("employee", employee);
                request.setAttribute("tag", "ok");
                request.getSession().setAttribute("emp_pass", emp_pass);
                request.getRequestDispatcher("info.jsp").forward(request, response);
            }
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }

    // 个人设置的查找信息
    private void search(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
    {
        request.setCharacterEncoding("UTF-8");
        String emp_no = (String) request.getSession().getAttribute("emp_no");
        EmployeeDAO employeeDAO = new EmployeeDAO();
        Employee employee = employeeDAO.findEmployeeByNo(emp_no);
        System.out.println("emp_no:  " + emp_no);
        request.setAttribute("employee", employee);
        request.getRequestDispatcher("info.jsp").forward(request, response);
    }
}
